﻿/*
创建人：zhunan
日期：2022.8.11
描述：热表理化试样维护js
*/
var page = {
  pagetotal: 0,
  pageNumber: 1,
  pageSize: 8,
  mainUrl: getRootPath() + "/Ashx/ReBiaoConainer/RbPhysicsAndChemicalTestHandler.ashx", //本页面后台路径
  //初始化页面
  init: function () {
    page.bind();
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    $("#tbGrid").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'productname', title: '零件号', width: 140 },
        { field: 'qty', title: '试验数量', width: 60 },
        { field: 'workflowstepname', title: '工序内容', width: 160 },
        { field: 'testitemname', title: '检验项目', width: 160 },
        {
          field: 'senddate', title: '送样时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        {
          field: 'estimatedate', title: '要求完成时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        { field: 'notes', title: '备注', width: 180 },
        { field: 'fullname', title: '创建人', hidden: true },
        { field: 'createdate', title: '创建时间', hidden: true },
        { field: 'createdate', title: '创建时间', hidden: true },
        { field: 'rbphysicsandchemicaltestid', title: 'rbphysicsandchemicaltestid', hidden: true }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        });
        customizedEasyuiPagination('pp'); //定制化分页
      },
      onCheck: function (rowIndex, rowData) {
        $("#txtInputProduct").textbox('setValue', rowData.productname);
        $("#txtQty").textbox('setValue', rowData.qty);
        $("#txtInputWorkflowStep").textbox('setValue', rowData.workflowstepname);
        $("#txtInputTestItem").textbox('setValue', rowData.testitemname);
        $("#txtInputSendDate").datebox('setValue', rowData.senddate);
        $("#txtInputEstimateDate").datebox('setValue', rowData.estimatedate);
        $("#txtInputNotes").textbox('setValue', rowData.notes);
      }
    });

    //分页事件
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize;
        page.pageNumber = pageNumber;
        page.search();
      }
    });

    // 查询按钮
    $("#btnSearch").on("click", function () {
      page.pageNumber = 1;
      page.search();
    });

    // 重置
    $("#btnReset").on('click', function () {
      page.resetData();
    });

    // 添加
    $("#btnAdd").on("click", function () {
      var data = page.validate();
      if (data.length !== 0) {
        page.insertData(data);
      }
    });

    // 修改
    $("#btnUpdate").on("click", function () {
      var checkedRowsData = $("#tbGrid").datagrid('getChecked');
      if (checkedRowsData.length === 0) {
        displayMessage("请勾选需要修改的记录！", false);
        return;
      }
      page.updateData(checkedRowsData);
    });

    // 删除
    $("#btnDelete").on("click", function () {
      var datas = $("#tbGrid").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要删除的记录！", false);
        return;
      }
      $.messager.confirm('删除前确认', '你确定要删除吗?', function (r) {
        // 确认删除
        if (r) {
          page.deleteData(datas);
        }
      });
    });
  },
  //初始化页面
  initData: function () { },
  // 查询
  search: function () {
    var formData = {
      action: "getData",
      pageSize: page.pageSize,
      pageNumber: page.pageNumber,
      productname: $("#txtProductname").textbox('getValue'),
      workflowstep: $("#txtWorkflowstep").textbox('getValue'),
      testitemname: $("#txtTestItem").textbox('getValue'),
      createEmployee: $("#txtCreateEmployee").textbox('getValue'),
      sendDateStart: $("#txtSendDateStart").datebox('getValue'),
      sendDateEnd: $("#txtSendDateEnd").datebox('getValue'),
      estimateDateStart: $("#txtEstimateDateStart").datebox('getValue'),
      estimateDateEnd: $("#txtEstimateDateEnd").datebox('getValue'),
      createDateStart: $("#txtCreateDateStart").datebox('getValue'),
      createDateEnd: $("#txtCreateDateEnd").datebox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=getData",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbGrid').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbGrid').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 插入
  insertData: function (postData) {
    var data = {
      action: "insertData",
      postData: JSON.stringify(postData)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=insertData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('保存成功！', true);
          $('#tbGrid').datagrid('loadData', []);
          page.clearInput();
          page.search(1);
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 更新
  updateData: function (checkedRowsData) {
    var postData = page.validate();
    if (!postData) {
      return;
    }
    var data = {
      action: "updateData",
      postData: JSON.stringify(postData),
      checkedRowsData: JSON.stringify(checkedRowsData)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=updateData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('修改成功', true);
          $('#tbGrid').datagrid('loadData', []);
          page.clearInput();
          page.dialogSearch(1);
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 删除
  deleteData: function (data) {
    var data = {
      action: "deleteData",
      willBeDeleteData: JSON.stringify(data)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=deleteData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('删除成功！', true);
          page.clearInput();
          $('#tbGrid').datagrid('loadData', []);
          page.dialogSearch(1);
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  validate: function () {
    var productname = $("#txtInputProduct").textbox('getValue');
    var qty = $("#txtQty").textbox('getValue');
    var workflowstepname = $("#txtInputWorkflowStep").textbox('getValue');
    var testitemname = $("#txtInputTestItem").textbox('getValue');
    var senddate = $("#txtInputSendDate").datebox('getValue');
    var estimatedate = $("#txtInputEstimateDate").datebox('getValue');
    var notes = $("#txtInputNotes").textbox('getValue');
    if (!productname) {
      displayMessage("零件号不能为空！", false);
      return [];
    }
    if (!/^[1-9]\d*$/.test(qty)) {
      displayMessage("数量为空或格式不正确！", false);
      return [];
    }
    if (!workflowstepname) {
      displayMessage("工序内容不能为空！", false);
      return [];
    }
    if (!testitemname) {
      displayMessage("检测项目不能为空！", false);
      return [];
    }
    if (isNaN(dayjs(senddate).$y)) {
      displayMessage("送样时间为空或格式不正确！", false);
      return [];
    }
    if (isNaN(dayjs(estimatedate).$y)) {
      displayMessage("要求完成时间为空或格式不正确！", false);
      return [];
    }
    var data = [{
      productname: productname,
      qty: qty,
      workflowstepname: workflowstepname,
      testitemname: testitemname,
      senddate: senddate,
      estimatedate: estimatedate,
      notes: notes
    }];
    return data;
  },
  resetData: function () {
    page.clearInput();
    clearMessage();
    $("#tbGrid").datagrid('loadData', { total: 0, rows: [] });
    page.pageNumber = 1;
    $("#txtProductname").textbox('clear');
    $("#txtWorkflowstep").textbox('clear');
    $("#txtTestItem").textbox('clear');
    $("#txtCreateEmployee").textbox('clear');
    $("#txtSendDateStart").datebox('clear');
    $("#txtSendDateEnd").datebox('clear');
    $("#txtEstimateDateStart").datebox('clear');
    $("#txtEstimateDateEnd").datebox('clear');
    $("#txtCreateDateStart").datebox('clear');
    $("#txtCreateDateEnd").datebox('clear');
  },
  clearInput: function () {
    $("#txtInputProduct").textbox('clear');
    $("#txtQty").textbox('clear');
    $("#txtInputWorkflowStep").textbox('clear');
    $("#txtInputTestItem").textbox('clear');
    $("#txtInputSendDate").datebox('clear');
    $("#txtInputEstimateDate").datebox('clear');
    $("#txtInputNotes").textbox('clear');
  }
};

page.init();